#pragma once

#include <tchar.h>
typedef unsigned short short16;
typedef unsigned char char8;
typedef unsigned int int32;

inline short16 COLOR(int32 color)
{
  int r=(color&0xff0000) >> 19;
  int g=(color&0xff00) >> 10;
  int b=(color&0xff) >> 3;
  
  return (r<<11) | (g<<5) | b;
}

inline short16 darkenColor(short16 color)
{
  return (color>>1)&0x7BEF;
}

inline short16 whitenColor(short16 color)
{
  return ((color>>1)&0x7BEF)+0x7BEF;
}


//single multiplication 16-bit color alpha blending... pretty cool huh?
//THIS IS FAST! :D
inline int colorInterpol(short16 x, short16 y, int n)
{
  int32 c1=(x&0xF81F) | ((x&0x7E0)<<16);
  int32 c2=(y&0xF81F) | ((y&0x7E0)<<16);
  int32 c=(c1+((c2-c1)*n>>5))&0x7E0F81F;
  return c|(c>>16);
}


template <class T>
inline T MIN(T t1, T t2)
{
  return t1<t2 ? t1 : t2;
}
template <class T>
inline T MAX(T t1, T t2)
{
  return t1>t2 ? t1 : t2;
}

template <class T>
inline T ABS(T &t)
{
  if (t<0) return -t; else return t;
}

class LRect
{
public:
  LRect() {x=y=w=h;}
  LRect(int _x, int _y, int _w, int _h)
  {
    x=_x; y=_y; w=_w; h=_h;
  }
  int x,y,w,h;
  void inflate(int i)
  {
    x-=i; y-=i; w+=i*2; h+=i*2;
  }
  
  int right() {
    return x+w;
  }
  int bottom()
  {
    return y+h;
  }
  
  bool isInside(int px, int py)
  {
    if (px>=x && py>=y && px<x+w && py<y+h)
      return true;
    else
      return false;
  }
  bool isRectInside(LRect &other)
  {
    return (other.x >= x && other.y>=y && other.right()<=right() && other.bottom()<=bottom());
  }
  void setSize(int _w, int _h)
  {
    w=_w; h=_h;
  }
  void setPos(int _x, int _y)
  {
    x=_x; y=_y;
  }
};


/*
inline LRect unionRect(LRect &r1, LRect &r2)
{
  //return LRect(MIN(r1.x,r2.x),MIN(r1.y,r2.y));
}*/